package com.lin.services.impl;

import com.lin.dao.UserDao;
import com.lin.pojo.User;
import com.lin.services.UserService;
import com.lin.utils.MD5Util;
import com.lin.utils.ShiroUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName:LoginServiceImpl
 * Package:com.lin.services.impl
 * Author: hellcat
 * Date: 2019/12/16
 * ♣♣♣
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Set<String> listUserRole(Integer userId) {
        Integer storeId = ShiroUtil.getUser().getStoreId();
        List<String> roles = userDao.getUserRoleList(userId, storeId);
        Set<String> rolesSet = new HashSet<>();
        for (String role : roles) {
            if (StringUtils.isNotBlank(role)) {
                rolesSet.addAll(Arrays.asList(role.trim().split(",")));
            }
        }
        System.out.println(rolesSet);
        return rolesSet;
    }

    @Override
    public Set<String> listUserAuthority(Integer userId) {
        Integer storeId = ShiroUtil.getUser().getStoreId();
        Set<String> authoritySet = new HashSet<>();
        if (storeId == null) {
            storeId = -1;
        }
        List<String> authorises = userDao.listUserAuthority(userId, storeId);
        for (String authority : authorises) {
            if (StringUtils.isNotBlank(authority)) {
                authoritySet.addAll(Arrays.asList(authority.trim().split(",")));
            }
        }
        return authoritySet;
    }

    @Override
    public User getUserByUsername(String username) {
        User user = userDao.getUserByUsername(username);
        if (user != null && user.getStoreId() != null) {
            user.setRole(userDao.getUserRoleList(user.getId(), user.getStoreId()).toString());
        }
        return user;
    }

    @Override
    public User getUserById(Integer userId) {
        return userDao.getUserById(userId);
    }

    @Override
    public Map<String, Object> registerCheck(User user) {
        Map<String, Object> map = new HashMap<>();
        try {
            String regex = "^[a-z0-9A-Z]+$";
            if (!user.getAccountNumber().trim().matches(regex)) {
                map.put("msg", "账号只能为数字/字母");
                return map;
            }
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
            Matcher m = p.matcher(user.getPhone().trim());
            if (!m.matches()) {
                map.put("msg", "手机号不规范");
                return map;
            }
            Pattern p2 = Pattern.compile("^(?=.*\\d)(?=.*[A-Za-z])[\\x21-\\x7e]{4,16}$");
            Matcher m2 = p2.matcher(user.getPwd().trim());
            if (!m2.matches()) {
                map.put("msg", "密码不规范");
                return map;
            }
            user.setPwd(MD5Util.encrypted(user.getAccountNumber(), user.getPwd().trim()));
            if ("".equals(user.getName().trim())) {
                map.put("msg", "姓名不规范");
                return map;
            }
            User userByUsername = userDao.getUserByUsername(user.getAccountNumber());
            if (userByUsername != null) {
                map.put("msg", "账号已存在");
                return map;
            }
            userDao.insertStaff(user);
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "注册失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }
}
